package com.meelive.ingkee.push;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;

import com.meelive.ingkee.push.model.InkePushType;

import java.util.ArrayList;

import static com.meelive.ingkee.push.model.Constance.TAG;

/**
 * Create by: fanfeiyue
 * on: 2019-08-22
 * desc:
 */
public class InkePushConfig {

    private String hwAppId;
    private String miAppId;
    private String miAppKey;
    private String mzAppId;
    private String mzAppKey;
    private String oppoAppKey;
    private String oppoAppSecret;
    private int vivoAppId;
    private String vivoAppKey;
    private String jPushAppKey;
    private ArrayList<InkePushType> enabledPushTypes;

    private InkePushConfig() {
    }

    public String getHwAppId() {
        return hwAppId;
    }

    public String getMiAppId() {
        return this.miAppId;
    }

    public String getMiAppKey() {
        return this.miAppKey;
    }

    public String getMzAppId() {
        return this.mzAppId;
    }

    public String getMzAppKey() {
        return this.mzAppKey;
    }

    public String getOppoAppKey() {
        return this.oppoAppKey;
    }

    public String getOppoAppSecret() {
        return this.oppoAppSecret;
    }

    public int getVivoAppId() {
        return vivoAppId;
    }

    public String getVivoAppKey() {
        return vivoAppKey;
    }

    public String getjPushAppKey() {
        return jPushAppKey;
    }

    public ArrayList<InkePushType> getEnabledPushTypes() {
        return this.enabledPushTypes;
    }

    @Override
    public String toString() {
        return "InkePushConfig{" +
                "hwAppId='" + hwAppId + '\'' +
                ", miAppId='" + miAppId + '\'' +
                ", miAppKey='" + miAppKey + '\'' +
                ", mzAppId='" + mzAppId + '\'' +
                ", mzAppKey='" + mzAppKey + '\'' +
                ", oppoAppKey='" + oppoAppKey + '\'' +
                ", oppoAppSecret='" + oppoAppSecret + '\'' +
                ", vivoAppId='" + vivoAppId + '\'' +
                ", vivoAppKey='" + vivoAppKey + '\'' +
                ", jPushAppKey='" + jPushAppKey + '\'' +
                ", enabledPushTypes=" + enabledPushTypes +
                '}';
    }

    public static class Builder {
        private Context context;

        private String hwAppId;
        private String miAppId;
        private String miAppKey;
        private String mzAppId;
        private String mzAppKey;
        private String oppoAppKey;
        private String oppoAppSecret;
        private int vivoAppId;
        private String vivoAppKey;
        private String jPushAppKey;
        private String jPushChannel;
        private ArrayList<InkePushType> enabledPushTypes = new ArrayList();

        public Builder(Context context) {
            this.context = context.getApplicationContext();
        }

        public Builder(Context context, InkePushConfig config) {
            this(context);

            if (config == null) {
                return;
            }

            if (config.enabledPushTypes.contains(InkePushType.MIPUSH)) {
                enableMiPush();
            }

            if (config.enabledPushTypes.contains(InkePushType.HMSPUSH)) {
                enableHWPush();
            }

            if (config.enabledPushTypes.contains(InkePushType.VIVOPUSH)) {
                enableVivoPush();
            }

            if (config.enabledPushTypes.contains(InkePushType.OPPOPUSH)) {
                enableOppoPush();
            }

            if (config.enabledPushTypes.contains(InkePushType.MEIZUPUSH)) {
                enableMeiZuPush();
            }

            if (config.enabledPushTypes.contains(InkePushType.JPUSH)) {
                enableJPush();
            }
        }

        public Builder enableMiPush() {
            try {
                ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(
                        context.getPackageName(), PackageManager.GET_META_DATA);
                miAppId = appInfo.metaData.getString("com.mi.push.app_id").split("=")[1];
                miAppKey = appInfo.metaData.getString("com.mi.push.app_key").split("=")[1];
                if (TextUtils.isEmpty(miAppId) || TextUtils.isEmpty(miAppKey)) {
                    Log.e(TAG, "enableMiPush fail: miAppId = " + miAppId + "miAppKey = " + miAppKey);
                    return this;
                }
                this.enabledPushTypes.add(InkePushType.MIPUSH);
            } catch (PackageManager.NameNotFoundException e) {
                Log.e(TAG, "mi NameNotFoundException: " + e.getMessage());
            } catch (NullPointerException e) {
                Log.e(TAG, "NullPointerException: miAppId = " + miAppId + ", miAppKey = " + miAppKey);
            } catch (ArrayIndexOutOfBoundsException e) {
                Log.e(TAG, "java.lang.ArrayIndexOutOfBoundsException: miAppId is null");
            }
            return this;
        }

        public Builder enableHWPush() {
            try {
                ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(
                        context.getPackageName(), PackageManager.GET_META_DATA);
                hwAppId = appInfo.metaData.getString("com.huawei.hms.client.appid").split("=")[1];
                if (TextUtils.isEmpty(hwAppId)) {
                    Log.e(TAG, "enableHMPush fail: hwAppId = " + hwAppId);
                    return this;
                }
                this.enabledPushTypes.add(InkePushType.HMSPUSH);
            } catch (PackageManager.NameNotFoundException e) {
                Log.e(TAG, "huawei NameNotFoundException: " + e.getMessage());
            } catch (NullPointerException e) {
                Log.e(TAG, "NullPointerException: hwAppId = " + miAppId);
            } catch (ArrayIndexOutOfBoundsException e) {
                Log.e(TAG, "java.lang.ArrayIndexOutOfBoundsException: hwAppId is null");
            }
            return this;
        }

        public Builder enableMeiZuPush() {
            try {
                ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(
                        context.getPackageName(), PackageManager.GET_META_DATA);
                mzAppId = appInfo.metaData.getString("com.meizu.push.app_id").split("=")[1];
                mzAppKey = appInfo.metaData.getString("com.meizu.push.app_key");
                if (TextUtils.isEmpty(mzAppId) || TextUtils.isEmpty(mzAppKey)) {
                    Log.e(TAG, "enableMeiZuPush fail: mzAppId = " + mzAppId + "mzAppKey = " + mzAppKey);
                    return this;
                }
                this.enabledPushTypes.add(InkePushType.MEIZUPUSH);
            } catch (PackageManager.NameNotFoundException e) {
                Log.e(TAG, "meizu NameNotFoundException: " + e.getMessage());
            } catch (NullPointerException e) {
                Log.e(TAG, "NullPointerException: mzAppId is null.");
            } catch (ArrayIndexOutOfBoundsException e) {
                Log.e(TAG, "java.lang.ArrayIndexOutOfBoundsException: mzAppId is null");
            }
            return this;
        }

        public Builder enableOppoPush() {
            try {
                ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(
                        context.getPackageName(), PackageManager.GET_META_DATA);
                oppoAppKey = appInfo.metaData.getString("com.oppo.push.app_key");
                oppoAppSecret = appInfo.metaData.getString("com.oppo.push.app_secret");
                if (TextUtils.isEmpty(oppoAppKey) || TextUtils.isEmpty(oppoAppSecret)) {
                    Log.e(TAG, "enableOppoPush fail: oppoAppKey = " + oppoAppKey + "oppoAppSecret = " + oppoAppSecret);
                    return this;
                }
                this.enabledPushTypes.add(InkePushType.OPPOPUSH);
            } catch (PackageManager.NameNotFoundException e) {
                Log.e(TAG, "oppo NameNotFoundException: " + e.getMessage());
            }
            return this;
        }

        public Builder enableVivoPush() {
            try {
                ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(
                        context.getPackageName(), PackageManager.GET_META_DATA);
                vivoAppId = appInfo.metaData.getInt("com.vivo.push.app_id");
                vivoAppKey = appInfo.metaData.getString("com.vivo.push.api_key");
                if (vivoAppId == 0 || TextUtils.isEmpty(vivoAppKey)) {
                    Log.e(TAG, "enableVivoPush fail: vivoAppId = " + vivoAppId + "vivoAppKey = " + vivoAppKey);
                    return this;
                }
                this.enabledPushTypes.add(InkePushType.VIVOPUSH);
            } catch (PackageManager.NameNotFoundException e) {
                Log.e(TAG, "vivo NameNotFoundException: " + e.getMessage());
            }
            return this;
        }

        public Builder enableJPush() {
            try {
                ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(
                        context.getPackageName(), PackageManager.GET_META_DATA);
                jPushAppKey = appInfo.metaData.getString("JPUSH_APPKEY");
                jPushChannel = appInfo.metaData.getString("JPUSH_CHANNEL");
                if (TextUtils.isEmpty(jPushAppKey)) {
                    Log.e(TAG, "enableJPush fail: jPushAppKey = " + jPushAppKey);
                    return this;
                }
                this.enabledPushTypes.add(InkePushType.JPUSH);
            } catch (PackageManager.NameNotFoundException e) {
                Log.e(TAG, "jiguang NameNotFoundException: " + e.getMessage());
            }
            return this;
        }

        public InkePushConfig build() {
            InkePushConfig pushConfig = new InkePushConfig();
            pushConfig.hwAppId = this.hwAppId;
            pushConfig.miAppId = this.miAppId;
            pushConfig.miAppKey = this.miAppKey;
            pushConfig.mzAppId = this.mzAppId;
            pushConfig.mzAppKey = this.mzAppKey;
            pushConfig.oppoAppKey = this.oppoAppKey;
            pushConfig.oppoAppSecret = this.oppoAppSecret;
            pushConfig.vivoAppId = this.vivoAppId;
            pushConfig.vivoAppKey = this.vivoAppKey;
            pushConfig.jPushAppKey = this.jPushAppKey;
            pushConfig.enabledPushTypes = this.enabledPushTypes;
            return pushConfig;
        }
    }

}
